home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / fileutil.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  11KB  |  356 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from __future__ import with_statement
  5. import os
  6. import codecs
  7. from threading import RLock as Lock
  8. from time import time
  9. from path import path
  10. import Queue as Q
  11.  
  12. def tail(filename, maxbytes, encoding = None):
  13.     if maxbytes <= 0:
  14.         raise ValueError('maxbytes must be more than 0')
  15.     
  16.     seekargs = (-maxbytes, os.SEEK_END)
  17.     if hasattr(filename, 'read'):
  18.         f = filename
  19.         will_close = False
  20.     else:
  21.         filesize = os.stat(filename).st_size
  22.         if encoding is not None:
  23.             f = codecs.open(filename, 'rb', encoding)
  24.         else:
  25.             f = open(filename, 'rb')
  26.         if maxbytes > filesize:
  27.             seek = 0
  28.         else:
  29.             seek = filesize - maxbytes
  30.         seekargs = (seek,)
  31.         will_close = True
  32.     f.seek(*seekargs)
  33.     s = f.read()
  34.     if will_close:
  35.         f.close()
  36.     
  37.     return s
  38.  
  39.  
  40. def streamcopy(fobjin, fobjouts, limit = None, chunk = 4096):
  41.     if hasattr(fobjouts, 'write'):
  42.         fobjouts = [
  43.             fobjouts]
  44.     
  45.     
  46.     def writer(data):
  47.         for fobjout in fobjouts:
  48.             fobjout.write(data)
  49.         
  50.  
  51.     return functioncopy(fobjin.read, writer, limit, chunk)
  52.  
  53.  
  54. def functioncopy(reader, writer, limit = None, chunk = 4096):
  55.     if not callable(reader) or not callable(writer):
  56.         raise TypeError("Both 'reader' and 'writer' must be callable. Got (%r, %r) instead.", reader, writer)
  57.     
  58.     written = 0
  59.     if limit is not None:
  60.         sz_to_read = min(limit, chunk)
  61.     else:
  62.         limit = -1
  63.         sz_to_read = chunk
  64.     bytes = reader(sz_to_read)
  65.     while bytes:
  66.         writer(bytes)
  67.         limit -= len(bytes)
  68.         written += len(bytes)
  69.         if limit > 0:
  70.             sz_to_read = min(limit, chunk)
  71.         elif limit == 0:
  72.             break
  73.         else:
  74.             sz_to_read = chunk
  75.         bytes = reader(sz_to_read)
  76.     return written
  77.  
  78. CHUNKSIZE = 32768
  79.  
  80. def trim_file(fname, cap, newsize):
  81.     fobjin = None
  82.     fobjout = None
  83.     fname = path(fname)
  84.     if fname.size > cap:
  85.         
  86.         try:
  87.             fobjin = open(fname, 'rb')
  88.             fobjout = open(fname + '.new', 'wb')
  89.             fobjin.seek(-newsize, os.SEEK_END)
  90.             streamcopy(fobjin, fobjout, CHUNKSIZE)
  91.         finally:
  92.             for f in (fobjin, fobjout):
  93.                 if f is not None:
  94.                     f.close()
  95.                     continue
  96.             
  97.  
  98.         os.remove(fname)
  99.         os.rename(fobjout.name, fname)
  100.     
  101.  
  102.  
  103. class PausableStream(object):
  104.     
  105.     def __init__(self, stream):
  106.         self._lock = Lock()
  107.         self.paused = False
  108.         self.stream = stream
  109.         self._queue = Q.Queue()
  110.  
  111.     
  112.     def pause(self):
  113.         self.paused = True
  114.  
  115.     
  116.     def unpause(self):
  117.         if self._lock.acquire():
  118.             
  119.             try:
  120.                 while True:
  121.                     
  122.                     try:
  123.                         self.stream.write(self._queue.get_nowait())
  124.                     continue
  125.                     except Q.Empty:
  126.                         break
  127.                         continue
  128.                     
  129.  
  130.                     None<EXCEPTION MATCH>Q.Empty
  131.             finally:
  132.                 self._lock.release()
  133.  
  134.             self.paused = False
  135.         
  136.  
  137.     
  138.     def write(self, data):
  139.         if self.paused:
  140.             self._queue.put(data)
  141.         elif self._lock.acquire(0):
  142.             
  143.             try:
  144.                 self.stream.write(data)
  145.             finally:
  146.                 self._lock.release()
  147.  
  148.         else:
  149.             self._queue.put(data)
  150.         return len(data)
  151.  
  152.     
  153.     def flush(self):
  154.         if not self.paused:
  155.             self.unpause()
  156.         
  157.         return self.stream.flush()
  158.  
  159.     
  160.     def close(self):
  161.         return self.stream.close()
  162.  
  163.     
  164.     def tell(self):
  165.         return self.stream.tell()
  166.  
  167.  
  168.  
  169. class SwappableStream(PausableStream):
  170.     
  171.     def start_swap(self):
  172.         self.pause()
  173.         self.stream.flush()
  174.         self.stream.close()
  175.  
  176.     
  177.     def finish_swap(self, newstream):
  178.         self.stream = newstream
  179.         self.unpause()
  180.  
  181.  
  182.  
  183. class LimitedFileSize(SwappableStream):
  184.     
  185.     def __init__(self, fname, filesize_limit, resize, initmode = 'wb'):
  186.         fobj = open(fname, initmode)
  187.         if resize > filesize_limit:
  188.             raise ValueError('resize must be smaller than filesize_limit. (resize=%r, filesize_limit=%r)', resize, filesize_limit)
  189.         
  190.         SwappableStream.__init__(self, fobj)
  191.         self._szlimit = filesize_limit
  192.         self._fname = fname
  193.         self._resize = resize
  194.  
  195.     
  196.     def write(self, data):
  197.         SwappableStream.write(self, data)
  198.         self.flush()
  199.         if os.path.getsize(self._fname) > self._szlimit:
  200.             self.start_swap()
  201.             
  202.             try:
  203.                 trim_file(self._fname, self._szlimit, self._resize)
  204.             finally:
  205.                 self.finish_swap(open(self._fname, 'ab'))
  206.  
  207.         
  208.  
  209.  
  210. from ratelimited import RateLimiter
  211.  
  212. class StreamLimiter(RateLimiter):
  213.     
  214.     def __init__(self, stream, limit = 4096, window = 5):
  215.         self.stream = stream
  216.         RateLimiter.__init__(self, self.stream.write, limit, window)
  217.  
  218.     
  219.     def write(self, data):
  220.         self.handle_data(data)
  221.  
  222.     
  223.     def flush(self):
  224.         return self.stream.flush()
  225.  
  226.     
  227.     def close(self):
  228.         return self.stream.close()
  229.  
  230.     
  231.     def tell(self):
  232.         return self.stream.tell()
  233.  
  234.     
  235.     def too_fast(self, data):
  236.         s = self.stream
  237.         s.write('Writing too fast: %r\n' % self.bps)
  238.         s.flush()
  239.  
  240.  
  241.  
  242. class DelayedStreamLimiter(StreamLimiter):
  243.     DELAY = 0.25
  244.     
  245.     def __init__(self, *a, **k):
  246.         StreamLimiter.__init__(self, *a, **k)
  247.         self._process_stop_time = 0
  248.  
  249.     
  250.     def handle_data(self, data):
  251.         should_write = None
  252.         if not StreamLimiter.handle_data(self, data):
  253.             now = time()
  254.             if self._process_stop_time == 0:
  255.                 if now - self._process_stop_time < self.DELAY:
  256.                     should_write = True
  257.                 else:
  258.                     self._process_stop_time = now
  259.                     should_write = False
  260.             elif now - self._process_stop_time < self.DELAY:
  261.                 should_write = True
  262.             else:
  263.                 should_write = False
  264.         else:
  265.             should_write = False
  266.             self._process_stop_time = 0
  267.         if should_write:
  268.             self.f_process(data)
  269.         
  270.         if should_write:
  271.             return True
  272.         elif self._process_stop_time == 0:
  273.             return True
  274.         else:
  275.             return False
  276.  
  277.  
  278.  
  279. class DisablingStream(object):
  280.     
  281.     def __init__(self, target):
  282.         self.target = target
  283.         self.write = self.write_enabled
  284.         self.flush = self.flush_enabled
  285.  
  286.     
  287.     def write_enabled(self, s):
  288.         
  289.         try:
  290.             self.target.write(s)
  291.         except:
  292.             self.disable()
  293.  
  294.  
  295.     
  296.     def flush_enabled(self):
  297.         
  298.         try:
  299.             self.target.flush()
  300.         except:
  301.             self.disable()
  302.  
  303.  
  304.     
  305.     def disable(self):
  306.         self.set_enabled(False)
  307.  
  308.     
  309.     def enable(self):
  310.         self.set_enabled(True)
  311.  
  312.     
  313.     def disabled(self, data = None):
  314.         pass
  315.  
  316.     
  317.     def set_enabled(self, val):
  318.         if val:
  319.             self.flush = self.flush_enabled
  320.             self.write = self.write_enabled
  321.         else:
  322.             self.flush = self.write = self.disabled
  323.  
  324.  
  325. if __name__ == '__main__':
  326.     from primitives import getrandbytes
  327.     data = getrandbytes(100)
  328.     half_len = len(data) / 2
  329.     from StringIO import StringIO
  330.     in_ = StringIO(data)
  331.     out = None
  332.     
  333.     def reset(i):
  334.         i.seek(0)
  335.         return StringIO()
  336.  
  337.     
  338.     def check(i, o, l, w):
  339.         if i.getvalue()[:l] == o.getvalue():
  340.             pass
  341.         return w == l
  342.  
  343.     __test_stream_copy = '>>> out = reset(in_); written = streamcopy(in_, out); check(in_, out, len(data), written)\nTrue\n>>> out = reset(in_); written = streamcopy(in_, out, chunk = len(data)); check(in_, out, len(data), written)\nTrue\n>>> out = reset(in_); written = streamcopy(in_, out, limit = half_len); check(in_, out, half_len, written)\nTrue\n>>> out = reset(in_); written = streamcopy(in_, out, limit = half_len, chunk = half_len+1); check(in_, out, half_len, written)\nTrue\n>>> out = reset(in_); written = streamcopy(in_, out, limit = half_len, chunk = half_len-1); check(in_, out, half_len, written)\nTrue\n'
  344.     __test_tail = '>>> in_.seek(0); tail(in_, 5) == in_.getvalue()[-5:]\nTrue\n>>> in_.seek(0); tail(in_, 1000) == in_.getvalue()\nTrue\n'
  345.     __test__ = dict(streamcopy = __test_stream_copy, tail = __test_tail)
  346.     import doctest
  347.     doctest.testmod(verbose = True)
  348.     import sys
  349.     f = DelayedStreamLimiter(sys.stdout, limit = 8, window = 1)
  350.     import time as time_mod
  351.     for i in range(20):
  352.         f.write(str(i) + '\n')
  353.         time_mod.sleep(0.04 * i)
  354.     
  355.  
  356.